package br.liveo.ndrawer.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import br.liveo.ndrawer.R;
import br.liveo.ndrawer.listener.ScrollListener;
import br.liveo.ndrawer.ui.activity.MainActivity;
import br.liveo.ui.PagerSlidingTabStrip;




public class DisplayFragment extends Fragment {

	private MainActivity mainActivity;
	private View rootView;
	private PagerSlidingTabStrip tabs;
	private ViewPager pager;
	private MyPagerAdapter adapter;
	private ScrollListener mScrollListener;

	public static DisplayFragment getInstance(MainActivity activity,
			ScrollListener scrollListener) {
		DisplayFragment mViewPagerFragment = new DisplayFragment();
		mViewPagerFragment.mainActivity = activity;
		mViewPagerFragment.mScrollListener = scrollListener;

		return mViewPagerFragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		rootView = inflater
				.inflate(R.layout.fragment_display, container, false);

		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		tabs = (PagerSlidingTabStrip) rootView.findViewById(R.id.tabs);
		pager = (ViewPager) rootView.findViewById(R.id.pager);
		adapter = new MyPagerAdapter(getChildFragmentManager());

		tabs.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		pager.setOffscreenPageLimit(3);
		pager.setAdapter(adapter);

		final int pageMargin = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
						.getDisplayMetrics());
		pager.setPageMargin(pageMargin);

		tabs.setViewPager(pager);
	}

	public class MyPagerAdapter extends FragmentPagerAdapter {

		private final String[] TITLES = { "Categories", "Home", "Top Paid" };

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return TITLES[position];
		}

		@Override
		public int getCount() {
			return TITLES.length;
		}

		@Override
		public Fragment getItem(int position) {
			return SuperAwesomeCardFragment.newInstance(mainActivity, position,
					mScrollListener);
		}

	}

	public void translateTab(int y) {
		tabs.setTranslationY(y);
	}

}
